package com.example.esp8266;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class LineNodeView extends View {

    private int mWidth;
    private int mHeight;

    private final Paint linePaint = new Paint();

    private boolean isTop = false;
    private boolean isEnd = false;

    public LineNodeView(Context context) {
        super(context);
    }

    public LineNodeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        linePaint.setColor(Color.BLACK);
        linePaint.setStrokeWidth(2f);
        linePaint.setAntiAlias(true);
        linePaint.setStyle(Paint.Style.FILL);
    }

    public LineNodeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public LineNodeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (isTop){
            canvas.drawLine(mWidth/2f,mHeight/2f,mWidth/2f,mHeight,linePaint);
            canvas.drawCircle(mWidth/2f,mHeight/2f,4f,linePaint);
        }else if (isEnd){
            canvas.drawLine(mWidth/2f,0,mWidth/2f,mHeight/2f,linePaint);
            canvas.drawCircle(mWidth/2f,mHeight/2f,4f,linePaint);
        }else {
            canvas.drawLine(mWidth/2f,0,mWidth/2f,mHeight,linePaint);
            canvas.drawCircle(mWidth/2f,mHeight/2f,4f,linePaint);
        }
    }

    public boolean isTop() {
        return isTop;
    }

    public void setTop(boolean top) {
        isTop = top;
    }

    public boolean isEnd() {
        return isEnd;
    }

    public void setEnd(boolean end) {
        isEnd = end;
    }
}
